﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BaseHelper.Data
{
    public class M:DBHelper
    {
        private string _tableName;
        private Hashtable _ht=new Hashtable();
        private string _id;
        private static DBInfo _db;
        
        public static DBInfo DBInfo
        {
            set { 
                _db = value;
            }
            get { return _db; }
        }
        
        public static string FixTableName
        {
            set {  _db.FixTableName = value; }
        }

        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public M(bool isKeepLive = false)
        {
            Init(_db,isKeepLive);
        }

        public M(DBInfo db, bool isKeepLive = false)
        {
            _db = db;
            Init(_db,isKeepLive);
        }


        public M(DBInfo db, string tableName)
        {
            _db = db;
            Init(_db);

            Table(tableName);   
        }


        ~M()
        {
            this.Close();
            GC.SuppressFinalize(this);
        }

        public M(string tableName,bool isKeepLive=false)
        {
            if(!base.IsInit)
                Init(_db, isKeepLive);

            Table(tableName);            
        }

        //public M(string tableName, string id)
        //{
        //    Init(_db);
        //    _id = id;

        //    Table(tableName);         
        //}

        //public M(string tableName, int id)
        //{
        //    Init(_db);
        //    _id = id.ToString();

        //    Table(tableName); 
        //}

        public M Table(string tableName)
        {
            if (tableName.ToLower().Contains("join"))
                _tableName = tableName;
            else
                _tableName =  _db.FixTableName + tableName;
            _ht.Clear();
            return this;
        
        }

        public void Set(string key, string value)
        {
            _ht[key] = value;
        }

        public int Insert(Hashtable ht, bool hasBinaryField = false)
        {
            return base.Insert(_tableName, ht, hasBinaryField);
        }

        public Hashtable Get(string id="")
        {
            id = base.Filter(id).Trim();            
            Hashtable[] hts = base.Select(_tableName, "id='" + (id==""?_id:id) + "'");
            if (hts != null)
                return hts[0];

            return null;
        }

        public Hashtable Get(Hashtable ht,string field="*",bool isvalue=false)
        {
            if (ht == null)
                return null;

            string where = "";
            foreach (string key in ht.Keys)
            {
                if (where != "")
                    where += " and ";

                if (isvalue)
                {
                    if (ht[key] != null && ht[key].ToString() != "")
                    {
                        where += "`" + key + "` " + ht[key].ToString() + " ";
                    }
                }
                else
                {
                    if (ht[key] != null)
                        where += "`" + key + "`='" + ht[key].ToString() + "'";
                    else
                        where += "`" + key + "`=''";
                }
            }   

            Hashtable[] hts = base.Select(_tableName, where, field);
            if (hts != null)
                return hts[0];

            return null;
        }
        
        public Hashtable Get(string key, string value,string field="*")
        {
            Hashtable[] hts=base.Select(_tableName, key + "='" + value + "' limit 1",field);
            if (hts != null)
                return hts[0];
            return null;
        }

        public Hashtable Get(string key, string value, string where, string field = "*")
        {
            if (where.Length > 0)
                where = "  and " + where;
            Hashtable[] hts = base.Select(_tableName, key + "='" + value + "'" + where + " limit 1", field);
            if (hts != null)
                return hts[0];
            return null;
        }

        public Hashtable[] Where(string where, params object[] obj)
        {
            where = string.Format(where, obj);
            return base.Select(_tableName, where);
        }

        public Hashtable[] Join(string table, string where,string field)
        {
            return base.Select(table, where, field);
        }

        public Hashtable[] Select(Hashtable ht, string field = "*", bool isValue=false,string orderby="",string groupby="")
        {
            if (ht == null)
                return null;

            string where = "";
            if (groupby != "")
                groupby = " group by " + groupby + " ";
            foreach (string key in ht.Keys)
            {
                if (where != "")
                    where += " and ";

                if (isValue)
                {
                    if (ht[key] != null && ht[key].ToString() != "")
                    {
                        where +=  key +  ht[key].ToString() + " ";
                    }
                }
                else
                {
                    if (ht[key] != null)
                        where +=  key + "='" + ht[key].ToString() + "'";
                    else
                        where +=  key + "=''";
                }
            }                            

            return base.Select(_tableName, where + groupby, field,orderby) ;
        }

        public Hashtable[] Select(string where="", string field = "*", string orderby = "",int page=-1,int pageSize=30)
        {
            return base.Select(_tableName, where, field, orderby, page, pageSize);
        }

        public Hashtable[] SelectByID(int id)
        {
            return base.Select(_tableName, "id='" + id.ToString() + "'"); 
        }

        public Hashtable[] SelectByID(string id)
        {
            return base.Select(_tableName, "id='" + id.ToString() + "'");
        }

        public Hashtable[] SelectByID()
        {
            return base.Select(_tableName, "id='" + _id + "'");
        }

        public Hashtable[] SelectByKey(string key,string value,string field = "*",string orderby="")
        {
            return base.Select(_tableName,key + "='" + value + "'",field,orderby);
        }

        public bool IsSelectHasRow(string where="")
        {
            return base.IsSelectHasRow(_tableName, where);
        }

        public bool IsSelectHasRow(string where, params object[] obj)
        {
            where = string.Format(where, obj);
            return base.IsSelectHasRow(_tableName, where);
        }

        public int Count()
        {
            Hashtable[] ht = base.Select(_tableName, "", "count(*) as datarow");
            if (ht != null)
            {
                return Convert.ToInt32(ht[0]["datarow"]);
            }

            return -1;
        }

        public int Count(string where)
        {
            Hashtable[] ht = base.Select(_tableName, where,"count(*) as datarow");
            if (ht != null)
            {
                return Convert.ToInt32(ht[0]["datarow"]);
            }

            return -1;
        }

        public int Count(string where, params object[] obj)
        {
            return Count(string.Format(where, obj));
        }

        public int Update(Hashtable ht=null, string where = "", bool hasBinaryField = false,bool isValue=false)
        {
            if(ht!=null)
                return base.Update(_tableName, ht, where, hasBinaryField, isValue);
            else if(_ht!=null && _id!="")
                return base.Update(_tableName, ht, "id='" + _id + "'", hasBinaryField, isValue);

            return 0;
        }

        public int UpdateByID(string id, string key, string value)
        {
            Hashtable ht = new Hashtable();
            ht[key] = value;
            return base.Update(_tableName, ht, "id='" + id + "'", false);
        }

        public int UpdateByID(string id,Hashtable ht, bool hasBinaryField = false)
        {
            return base.Update(_tableName, ht, "id='" + id +"'", hasBinaryField);
        }

        public int UpdateByGuid(string guid, Hashtable ht, bool hasBinaryField = false)
        {
            return base.Update(_tableName, ht, "guid='" + guid + "'", hasBinaryField);
        }
        
        public int Delete(string where = "")
        {
            if(_id!="")
                return base.Delete(_tableName, "id='" + _id + "'");
            else
                return base.Delete(_tableName, where);
        }

        public int DeleteByID(string id)
        {
            return base.Delete(_tableName, "id='" + id + "'");
        }

        public int DeleteByGuid(string guid = "")
        {
            return base.Delete(_tableName, "guid='" + guid + "'");
        }

    }
}
